package com.hjs.servlet.login;

import com.hjs.bean.UserView;
import com.hjs.bean.UsersInfo;
import com.hjs.mapper.ViewMapper;
import com.hjs.tools.Mapper;
import com.hjs.tools.RequestUtil;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Slf4j
@WebServlet("/user/info")
public class UserInfo extends HttpServlet {

    protected void doPost(HttpServletRequest request, HttpServletResponse response) {
        RequestUtil requestUtil = new RequestUtil(request, response);
        Mapper<ViewMapper> viewMapperMapper = new Mapper<>(ViewMapper.class);
        try {
            ViewMapper mapper = viewMapperMapper.getMapper();
            UserView userView = mapper.getOneUserViewById(requestUtil.getString("id"));
            if (userView != null) {
                UsersInfo userInfo = new UsersInfo(userView);
                requestUtil.responseWrite(Mapper.toJsonString(userInfo));
            } else {
                requestUtil.responseFail("获取用户信息失败", 5001);
            }

        } catch (Exception e) {
            log.error(e.toString());
            e.printStackTrace();
            requestUtil.responseFail("未知错误");
        } finally {
            viewMapperMapper.closeSession();
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) {

    }
}
